package io.adrainty.bolee.insurance.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.adrainty.bolee.insurance.models.bo.CoefficientBO;
import io.adrainty.bolee.insurance.mapper.CoefficientMapper;
import io.adrainty.bolee.insurance.models.entity.CoefficientEntity;
import io.adrainty.bolee.insurance.repository.ICoefficientRepository;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>CoefficientServiceRepositoryImpl</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description 保障项系数管理
 * @since 2025/8/11 10:23:35
 */

@Repository
public class CoefficientRepositoryImpl implements ICoefficientRepository {

    @Resource
    private CoefficientMapper coefficientMapper;

    @Override
    public IPage<CoefficientEntity> findCoefficientPage(Integer pageNum, Integer pageSize, CoefficientEntity searchDTO) {
        Page<CoefficientBO> page = new Page<>(pageNum, pageSize);
        Wrapper<CoefficientBO> wrapper = getWrapper(searchDTO);
        Page<CoefficientBO> selectPage = coefficientMapper.selectPage(page, wrapper);
        return selectPage.convert(item -> BeanUtil.toBean(item, CoefficientEntity.class));
    }

    @Override
    public List<CoefficientEntity> findCoefficientList(CoefficientEntity entity) {
        Wrapper<CoefficientBO> wrapper = getWrapper(entity);
        List<CoefficientBO> selectList = coefficientMapper.selectList(wrapper);
        return BeanUtil.copyToList(selectList, CoefficientEntity.class);
    }

    @Override
    public boolean createCoefficient(CoefficientEntity entity) {
        CoefficientBO coefficientBO = BeanUtil.toBean(entity, CoefficientBO.class);
        boolean result = coefficientMapper.insert(coefficientBO) > 0;
        if (!result) return false;
        BeanUtil.copyProperties(coefficientBO, entity);
        return true;
    }

    @Override
    public boolean updateCoefficient(CoefficientEntity entity) {
        CoefficientBO coefficientBO = BeanUtil.toBean(entity, CoefficientBO.class);
        return coefficientMapper.updateById(coefficientBO) > 0;
    }

    @Override
    public boolean deleteCoefficient(List<String> deleteIdList) {
        return coefficientMapper.deleteByIds(deleteIdList) > 0;
    }

    @Override
    public CoefficientEntity findByCoefficientKey(String coefficientKey) {
        Wrapper<CoefficientBO> wrapper = Wrappers.lambdaQuery(CoefficientBO.class)
                .eq(CoefficientBO::getCoefficientKey, coefficientKey);
        CoefficientBO selectOne = coefficientMapper.selectOne(wrapper);
        if (selectOne == null) return null;
        return BeanUtil.toBean(selectOne, CoefficientEntity.class);
    }

    private static Wrapper<CoefficientBO> getWrapper(CoefficientEntity searchDTO) {
        return Wrappers.lambdaQuery(CoefficientBO.class)
                .eq(StringUtils.isNotBlank(searchDTO.getCoefficientKey()), CoefficientBO::getCoefficientKey, searchDTO.getCoefficientKey())
                .eq(StringUtils.isNotBlank(searchDTO.getCoefficientKeyName()), CoefficientBO::getCoefficientKeyName, searchDTO.getCoefficientKeyName())
                .eq(StringUtils.isNotBlank(searchDTO.getCoefficientType()), CoefficientBO::getCoefficientType, searchDTO.getCoefficientType())
                .eq(searchDTO.getDataState() != null, CoefficientBO::getDataState, searchDTO.getDataState())
                .orderByAsc(CoefficientBO::getSortNo)
                .orderByDesc(CoefficientBO::getCreatedTime);
    }
}
